/* 计算1-1/x+1/x*x… */
#include <stdio.h>
#include <sys/timeb.h>

int main(int argc, char **argv)
{
    struct timeb t1, t2;
    long t;
    double x, sum = 1, sum1;
    int i, j, n;
    printf("请输入x n：");
    scanf("%lf%d", &x, &n);
    ftime(&t1); /* 求得当前时间 */
    for (i = 1; i <= n; i++)
    {
        sum1 = 1;
        for (j = 1; j <= i; j++)
            sum1 = -sum1 / x;
        sum += sum1;
    }
    ftime(&t2);                                                 /* 求得当前时间 */
    t = (t2.time - t1.time) * 1000 + (t2.millitm - t1.millitm); /* 计算时间差 */
    printf("sum=%lf 用时%ld毫秒\n", sum, t);

    return 0;
}
/*
编译：make
运行：./time_complexity1
结果：
请输入x n：3000 10000
sum=0.999667 用时265毫秒
清除编译产物：make clean
*/

/******************** end of file **********************/
